package com.xuanyuanxinyu.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.xuanyuanxinyu.view.R

class ListDifferAdapter : ListAdapter<User, ListDifferAdapter.ViewHolder>(
    DiffItemCallback()
) {
    override fun onCreateViewHolder(parent : ViewGroup,viewType : Int) : ViewHolder {
        val itemView : View = LayoutInflater.from(parent.context).inflate(R.layout.item_user_list,parent,false)
        return ViewHolder(itemView)
    }
    
    override fun onBindViewHolder(holder : ViewHolder, position : Int) {
        holder.bind(getItem(position))
    }
    
    class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
        private var tvName : TextView? = null
        private var tvAge : TextView? = null
        
        init {
            tvName = itemView.findViewById(R.id.tvName)
            tvAge = itemView.findViewById(R.id.tvId)
        }
        
        fun bind(user : User) {
            tvName?.text = user.name
            tvAge?.text = user.id.toString()
        }
    }
    
    /**
     * 直接使用它更新数据就行了
     */
    override fun submitList(list : MutableList<User>?) {
        super.submitList(list)
    }
}